<!--  -->
<template>
  <div>
    <div class="pending">
      <!-- 搜索 -->
      <div class="formSearch">
        <el-form :inline="true" :model="formInline" class="demo-form-inline">
          <el-form-item label="商品名称">
            <el-input v-model="formInline.productName" placeholder="请输入商品名称" />
          </el-form-item>
          <el-form-item label="上架状态">
            <el-select v-model="formInline.isShelve" placeholder="请选择上架状态">
              <el-option label="全部" :value="null" />
              <el-option label="上架" value="1" />
              <el-option label="下架" value="0" />
            </el-select>
          </el-form-item>
          <el-form-item label="排序">
            <el-select v-model="formInline.orderBy" placeholder="排序">
              <el-option label="倒序" value="1" />
              <el-option label="正序" value="0" />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" plain @click="search">查询</el-button>
            <el-button type="success" plain @click="add">新增</el-button>
          </el-form-item>
        </el-form>
      </div>
      <!-- 表格 -->
      <div class="tableBox">
        <el-table
          ref="multipleTable"
          :data="tableData"
          border
          :header-cell-style="{ background: '#EEF3FF', color: '#333333' }"
          tooltip-effect="dark"
          style="width: 100%"
        >
          <el-table-column prop="productId" label="商品id" show-overflow-tooltip />
          <el-table-column label="商品主图" width="150" align="center">
            <template slot-scope="scope">
              <img height="80" width="80" :src="scope.row.image " alt srcset>
            </template>
          </el-table-column>
          <el-table-column prop="productName" label="商品名称" width="220" />
          <el-table-column prop="price" label="售价" show-overflow-tooltip />
          <el-table-column prop="stock" label="库存" show-overflow-tooltip />
          <el-table-column prop="points" label="所需积分" show-overflow-tooltip />
          <el-table-column prop="createAt" label="创建时间" :formatter="formatData" width="180" />
          <el-table-column prop="isShelve" label="上架状态" show-overflow-tooltip>
            <template slot-scope="scope">
              <span v-if="scope.row.isShelve==0">未上架</span>
              <span v-if="scope.row.isShelve==1">已上架</span>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="200">
            <template slot-scope="scope">
              <div class="btnList">
                <el-button type="text" @click="edit(scope.row)">编辑</el-button>
                <el-button v-if="scope.row.shelveState!=1" type="text" @click="del(scope.row)">删除</el-button>
              </div>
            </template>
          </el-table-column>
        </el-table>
        <div class="fenye">
          <el-pagination
            :current-page="currentPage"
            :page-sizes="[10, 20, 50, 100]"
            :page-size="10"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </div>
      </div>
    </div>
  </div>
</template>

<script>
// 这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
// 例如：import 《组件名称》 from '《组件路径》';

import { uploadUrl } from '@/utils/request'
import {
  getList,
  deleteById
} from '@/api/pointCommodity'

import { formatTime } from '@/api/formatDate'

export default {
  components: {},
  data() {
    // 这里存放数据
    return {
      activeName: 'first',
      formInline: {
        productName: '', // 搜索字段
        isShelve: '', // 上架状态 1-上架 0-不上架 null-全部
        orderBy: '', // 排序
        pageNum: 1, // 当前页
        pageSize: 10
      },
      batchAdd: false,
      batchFileList: [],
      total: 1,
      tableData: [],
      currentPage: 1,
      categoryList: [],
      UploadUrl: uploadUrl,
      vipPriceVisible: false,
      productId: 0,
      mode: 1,
      productData: [],
      gradeList: []
    }
  },
  // 监听属性 类似于data概念
  computed: {},
  // 监控data中的数据变化
  watch: {},
  // 生命周期 - 创建完成（可以访问当前this实例）
  created() {
  },
  // 生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {
    this.getAll(this.formInline)
  },
  // 方法集合
  methods: {
    handleSizeChange(val) {
      console.log(val)
      this.formInline.pageSize = val
      this.getAll(this.formInline)
    },
    handleCurrentChange(val) {
      console.log(val)
      this.formInline.pageNum = val
      this.getAll(this.formInline)
    },
    //  查询
    search() {
      this.total = 1
      this.formInline.pageNum = 1
      this.getAll(this.formInline)
    },
    // 新增商品
    add() {
      this.$router.push({ name: 'saveCommodity' })
    },
    formatData(data) {
      const date = new Date(data.createAt)
      return formatTime(date, 'yyyy-MM-dd hh:mm')
    },
    // 编辑商品
    async edit(row) {
      this.$router.push({
        name: 'saveCommodity',
        query: { productId: row.productId }
      })
    },
    // 删除商品
    del(row) {
      this.$confirm('选中数据将被永久删除, 是否继续？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          deleteById(row.productId).then(res => {
            if (res.code === '200') {
              this.$message({
                type: 'success',
                message: '删除成功!'
              })
              this.getAll(this.formInline)
            }
          })
        })
        .catch(() => {
        })
    },

    // 初始化查询所有数据
    async getAll(formInline) {
      console.log(formInline)
      const res = await getList(formInline)
      console.log(res, '初始化')
      this.total = res.data.total
      this.tableData = res.data.list
    }
  }
}
</script>
<style lang='scss' scoped>
//@import url(); 引入公共css类
@import url("../../styles/elDialog.scss");

.pending {
  padding: 30px;
}

.fenye {
  margin-top: 20px;
}

.clickMe {
  color: #3a68f2;
  cursor: pointer;
}

.vipDialog {
  .priceTable {
    table {
      width: 100%;
      text-align: center;
      border-left: 1px solid #EBEEF5;
      border-bottom: 1px solid #EBEEF5;
      font-size: 14px;
      color: #606266;
      border-collapse: collapse;

      tr {
        border-top: 1px solid #EBEEF5;

        th {
          padding: 12px 0;
          background: #EEF3FF;
          color: #333;
          border-right: 1px solid #EBEEF5;
        }

        td {
          padding: 12px 0;
          border-right: 1px solid #EBEEF5;

          &:nth-child(1), &:nth-child(2) {
            width: 80px;
          }

          .el-input {
            width: 100px;
            margin-right: 10px;
          }
        }
      }
    }
  }
}
</style>
